package com.lyb.course.service.impl;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.lyb.course.domain.Course;
import com.lyb.course.domain.CourseDetail;
import com.lyb.course.domain.CourseMarket;
import com.lyb.course.dto.CourseSaveDTO;
import com.lyb.course.mapper.CourseDetailMapper;
import com.lyb.course.mapper.CourseMapper;
import com.lyb.course.mapper.CourseMarketMapper;
import com.lyb.course.query.CourseQuery;
import com.lyb.course.service.ICourseService;
import com.lyb.hrm.doc.CourseDoc;
import com.lyb.hrm.feignclients.ESFeignClient;
import com.lyb.hrm.util.AjaxResult;
import com.lyb.hrm.util.LoginInfo;
import com.lyb.hrm.util.PageList;
import com.lyb.hrm.util.UserContext;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements ICourseService {

    @Autowired
    private CourseDetailMapper courseDetailMapper ;

    @Autowired
    private CourseMarketMapper courseMarketMapper ;

    @Autowired
    private ESFeignClient esFeignClient;

    @Override
    public void saveOrUpdate(CourseSaveDTO courseSaveDTO) {
        // 保存课程
        Course course = courseSaveDTO.getCourse();

        // 课程状态
        course.setStatus(Course.STATUS_OFFLINE);
        // 开课时间
        Date date = new Date();
        course.setStartTime(date);
        // 节课时间
        course.setEndTime(date);

        // 机构id :
        LoginInfo loginInfo = UserContext.getLoginInfo();
        course.setTenantId(loginInfo.getTenantId());
        // 机构名字
        course.setTenantName(loginInfo.getTenantName());
        // 创建课程的用户id
        course.setUserId(loginInfo.getUserId());

        // 创建课程的用户名
        course.setUserName(loginInfo.getUserName());

        baseMapper.insert(course);

        // 保存课程详情
        CourseDetail courseDetail = courseSaveDTO.getCourseDetail();
        courseDetail.setId(course.getId());
        courseDetailMapper.insert(courseDetail);

        // 保存课程营销
        CourseMarket courseMarket = courseSaveDTO.getCourseMarket();

        courseMarket.setId(course.getId());
        courseMarket.setPriceOld(courseMarket.getPrice());

        // 截止时间
        courseMarket.setExpires(date);

        courseMarketMapper.insert(courseMarket);

    }

    @Override
    public void onLineCourse(Long id) {
        // 根据id查询课程
        Course course = baseMapper.selectById(id);

        // 判断课程的状态
        if(course == null || course.getStatus() != Course.STATUS_OFFLINE){
            throw new RuntimeException("课程状态不处于下线状态");
        }


        // 课程营销
        CourseMarket courseMarket = courseMarketMapper.selectById(id);
        CourseDetail courseDetail = courseDetailMapper.selectById(id);

        CourseDoc courseDoc = new CourseDoc();
        // 拷贝课程的属性到doc
        BeanUtils.copyProperties(course , courseDoc);
        // 拷贝营销的属性到doc
        BeanUtils.copyProperties(courseMarket , courseDoc);
        // 拷贝详情的属性到doc
        BeanUtils.copyProperties(courseDetail , courseDoc);
        // 其他需要处理的属性
        courseDoc.setChargeName(courseMarket.getCharge()==0?"免费":"收费");
        // 预览数量
        courseDoc.setViewCount(100);
        // 购买数量
        courseDoc.setBuyCount(200);

        AjaxResult ajaxResult = esFeignClient.save(courseDoc);
        if(ajaxResult.isSuccess()){
            // 修改Mysql中的课程状态为：上线
            course.setStatus(Course.STATUS_ONLINE);
            baseMapper.updateById(course);
        }else{
            throw new RuntimeException("课程上线失败");
        }
    }

    @Override
    public void offLineCourse(Long id) {
        // 根据id查询课程
        Course course = baseMapper.selectById(id);

        // 判断课程状态必须为上线
        if(course == null || course.getStatus() != Course.STATUS_ONLINE){
            throw new RuntimeException("课程为空，或者课程状态不处于“上线");
        }

        // 删除ES中的课程
        AjaxResult ajaxResult = esFeignClient.deleteById(id);
        if(!ajaxResult.isSuccess()){
            throw new RuntimeException("课程下线失败");
        }

        // 修改mysql中的课程状态为：下线
        course.setStatus(Course.STATUS_OFFLINE);
        baseMapper.updateById(course);
    }

    @Override
    public PageList<CourseDoc> queryCourses(CourseQuery courseQuery) {
        return null;
    }
}
